package com.qnbc.mystery.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * @Description 描述文档配置
 * @ClassName Knife4jConfiguration
 * @Author 大先生
 * @date 2022/5/25
 */
@Configuration
@EnableKnife4j
//@Import(BeanValidatorPluginsConfiguration.class)
public class Knife4jConfiguration {
    /**
     * 应用名称
     */
    @Value("${sysinfo.name}")
    private String appName;
    @Value("${sysinfo.version}")
    private String appVersion;
    /**
     * 用户TOKEN-key
     */
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;

    /**
     * 登录模块
     *
     * @return
     */
    @Bean(value = "defaultApiLogIn")
    public Docket defaultApiLogIn() {

        Contact contact = new Contact("清柠编程", "", "850976314@qq.com");
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title(appName + " RESTful APIs")
                        .description(appName + "接口")
                        .termsOfServiceUrl("http://www.xx.com/")
                        .contact(contact)
                        .version(appVersion)
                        .build())
                //分组名称
                .groupName("1、盲盒-" + appVersion + "版本")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.qnbc.mystery.controller"))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
//                .pathMapping("/venueReservation");//范围前缀
        return docket;
    }

//    /**
//     * 公共模块
//     * @return
//     */
//    @Bean(value = "defaultApiPub")
//    public Docket defaultApiPub() {
//
//        Contact contact = new Contact("大先生", "", "850976314@qq.com");
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                        .title(appName+" RESTful APIs")
//                        .description(appName+"接口")
//                        .termsOfServiceUrl("http://www.xx.com/")
//                        .contact(contact)
//                        .version(appVersion)
//                        .build())
//                //分组名称
//                .groupName("2、公共模块-"+appVersion+"版本")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.huichen.manager.controller.common"))
//                .paths(PathSelectors.any())
//                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
//        //.pathMapping("/dev");//范围前缀
//        return docket;
//    }
//
//    /**
//     * 系统模块
//     * @return
//     */
//    @Bean(value = "defaultApi")
//    public Docket defaultApi() {
//
//        Contact contact = new Contact("大先生", "", "850976314@qq.com");
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                        .title(appName+" RESTful APIs")
//                        .description(appName+"接口")
//                        .termsOfServiceUrl("http://www.xx.com/")
//                        .contact(contact)
//                        .version(appVersion)
//                        .build())
//                //分组名称
//                .groupName("3、系统模块-"+appVersion+"版本")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.huichen.manager.controller.system"))
//                .paths(PathSelectors.any())
//                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
//                //.pathMapping("/dev");//范围前缀
//        return docket;
//    }
//
//    /**
//     * 账户模块
//     * @return
//     */
//    @Bean(value = "defaultUser")
//    public Docket defaultUser() {
//
//        Contact contact = new Contact("大先生", "", "850976314@qq.com");
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                        .title(appName+" RESTful APIs")
//                        .description(appName+"接口")
//                        .termsOfServiceUrl("http://www.xx.com/")
//                        .contact(contact)
//                        .version(appVersion)
//                        .build())
//                //分组名称
//                .groupName("4、账户模块-"+appVersion+"版本")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.huichen.manager.controller.user"))
//                .paths(PathSelectors.any())
//                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
//        //.pathMapping("/dev");//范围前缀
//        return docket;
//    }
//
//    /**
//     * 支付模块
//     * @return
//     */
//    @Bean(value = "defaultPay")
//    public Docket defaultPay() {
//
//        Contact contact = new Contact("大先生", "", "850976314@qq.com");
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                        .title(appName+" RESTful APIs")
//                        .description(appName+"接口")
//                        .termsOfServiceUrl("http://www.xx.com/")
//                        .contact(contact)
//                        .version(appVersion)
//                        .build())
//                //分组名称
//                .groupName("5、支付模块-"+appVersion+"版本")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.huichen.manager.controller.pay"))
//                .paths(PathSelectors.any())
//                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
//        //.pathMapping("/dev");//范围前缀
//        return docket;
//    }
//
//    /**
//     * 权益模块
//     * @return
//     */
//    @Bean(value = "defaultApiCoupons")
//    public Docket defaultApiCoupons() {
//
//        Contact contact = new Contact("大先生", "", "850976314@qq.com");
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                        .title(appName+" RESTful APIs")
//                        .description(appName+"接口")
//                        .termsOfServiceUrl("http://www.xx.com/")
//                        .contact(contact)
//                        .version(appVersion)
//                        .build())
//                //分组名称
//                .groupName("6、权益模块-"+appVersion+"版本")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.huichen.coupons.controller"))
//                .paths(PathSelectors.any())
//                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
//        //.pathMapping("/dev");//范围前缀
//        return docket;
//    }
//
//    /**
//     * 权益模块
//     * @return
//     */
//    @Bean(value = "defaultApiProxy")
//    public Docket defaultApiProxy() {
//
//        Contact contact = new Contact("大先生", "", "850976314@qq.com");
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                        .title(appName+" RESTful APIs")
//                        .description(appName+"接口")
//                        .termsOfServiceUrl("http://www.xx.com/")
//                        .contact(contact)
//                        .version(appVersion)
//                        .build())
//                //分组名称
//                .groupName("7、代充帮充-"+appVersion+"版本")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.huichen.manager.controller.proxy"))
//                .paths(PathSelectors.any())
//                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
//        //.pathMapping("/dev");//范围前缀
//        return docket;
//    }
//    /**
//     * 微信公众号模块
//     * @return
//     */
//    @Bean(value = "defaultApiWx")
//    public Docket defaultApiWx() {
//
//        Contact contact = new Contact("大先生", "", "850976314@qq.com");
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                        .title(appName+" RESTful APIs")
//                        .description(appName+"接口")
//                        .termsOfServiceUrl("http://www.xx.com/")
//                        .contact(contact)
//                        .version(appVersion)
//                        .build())
//                //分组名称
//                .groupName("8、微信公众号-"+appVersion+"版本")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.huichen.manager.controller.wx"))
//                .paths(PathSelectors.any())
//                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
//        //.pathMapping("/dev");//范围前缀
//        return docket;
//    }


    /**
     * 配置模块
     * @return
     */
//    @Bean(value = "defaultConfig")
//    public Docket defaultConfig() {
//
//        Contact contact = new Contact("大先生", "", "850976314@qq.com");
//        Docket docket = new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                        .title(appName+" RESTful APIs")
//                        .description(appName+"接口")
//                        .termsOfServiceUrl("http://www.xx.com/")
//                        .contact(contact)
//                        .version(appVersion)
//                        .build())
//                //分组名称
//                .groupName("6、配置模块-"+appVersion+"版本")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.huichen.manager.controller.other"))
//                .paths(PathSelectors.any())
//                .build()
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts());
//        //.pathMapping("/dev");//范围前缀
//        return docket;
//    }

    /**
     * @param
     * @Description: 设置swagger文档中全局参数
     * @Date: 2020/9/11 10:15
     * @return: java.util.List<springfox.documentation.service.Parameter>
     */
    private List<Parameter> setHeaderToken() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder userId = new ParameterBuilder();
        userId.name(tokenHeader).description("用户TOKEN").modelRef(new ModelRef("string")).parameterType("header")
                .required(true).build();
        pars.add(userId.build());
        return pars;
    }


    private List<SecurityScheme> securitySchemes() {
        ApiKey key = new ApiKey(tokenHeader, tokenHeader, "header");


        return Collections.singletonList(new ApiKey(tokenHeader, tokenHeader, "header"));
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference(tokenHeader, authorizationScopes));
        return securityReferences;
    }
}
